package com.yunbao.project.basedata.controller;


import com.yunbao.core.respones.Result;
import com.yunbao.core.respones.ResultBuilder;
import com.yunbao.core.respones.ResultEnum;
import com.yunbao.project.basedata.entity.GoodsCategory;
import com.yunbao.project.basedata.service.IGoodsCategoryService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品分类 前端控制器
 * </p>
 *
 * @author 安能
 * @since 2019-09-16
 */
@RestController
@RequestMapping("goodsCategory")
public class GoodsCategoryController {


    @Resource
    IGoodsCategoryService goodsCategoryService;

    @PostMapping("insert")
    public Result insert(@RequestBody GoodsCategory goodsCategory) {
        goodsCategoryService.save(goodsCategory);
        return ResultBuilder.success(ResultEnum.ADD_SUCCESS);
    }

    @PostMapping("deleteById")
    public Result deleteById(Integer id) {

        goodsCategoryService.removeById(id);
        return ResultBuilder.success();
    }

    /**
     * 递归删除当前节点和子节点
     *
     * @param id
     * @return
     */
    @PostMapping("deleteRecursionById")
    public Result deleteRecursionById(Integer id) {

        goodsCategoryService.deleteRecursionById(id);
        return ResultBuilder.success(ResultEnum.DELETE_SUCCESS);
    }

    @PostMapping("updateById")
    public Result updateById(@RequestBody GoodsCategory goodsCategory) {

        goodsCategoryService.updateById(goodsCategory);
        return ResultBuilder.success();
    }

    @PostMapping("getById")
    public Result getById(Integer id) {

        GoodsCategory byId = goodsCategoryService.getById(id);
        return ResultBuilder.success(byId);
    }


    @PostMapping("getListLike")
    public Result getListLike() {

        List<GoodsCategory> list = goodsCategoryService.list();
        return ResultBuilder.success(list);
    }

    /**
     * <p>获取树形结构的商品分类</p>
     *
     * @return
     */
    @PostMapping("selectTreeList")
    public Result selectTreeList() {
        List<GoodsCategory> list = goodsCategoryService.selectTreeList(0);
        return ResultBuilder.success(list);
    }

}
